package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController {

    @Reference
    private AddressService addressService;

    //查找所有地址信息
    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<Address> list = addressService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //分页查询地址信息
    @RequestMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, addressService.findPage(queryPageBean));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //新增地址信息
    @RequestMapping("add")
    public Result add(@RequestBody Address address) {
        try {
            String latitude = address.getLatitude().substring(0, 9);
            String longitude = address.getLongitude().substring(0, 10);
            address.setLatitude(latitude);
            address.setLongitude(longitude);
            addressService.add(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    //删除地址信息
    @RequestMapping("del")
    public Result del(int id) {
        try {
            return addressService.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }
}
